using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class CashBankInDTO : INotifyPropertyChanged
    {
        private CoaDTO _CashBankAccount;
        private ObservableCollection<CashBankInDetailDTO> _CashBankInDetails;
        private CustomerDTO _Customer;
        private String _Description;
        private String _NoVoucher;


        private DateTime _Tanggal;
        private int _No;
        private decimal _TotalDebit;
        private decimal _Balance;
        private decimal _TotalKredit;

        public CashBankInDTO()
        {
            CashBankInDetails = new ObservableCollection<CashBankInDetailDTO>();
            Tanggal = DateTime.Now.Date;
        }

        [DataMember]
        public String NoVoucher
        {
            get { return _NoVoucher; }
            set
            {
                _NoVoucher = value;
                RaisePropertyChanged("NoVoucher");
            }
        }

        [DataMember]
        public DateTime Tanggal
        {
            get { return _Tanggal; }
            set
            {
                _Tanggal = value;
                RaisePropertyChanged("Tanggal");
            }
        }


       


        [DataMember]
        public CoaDTO CashBankAccount
        {
            get { return _CashBankAccount; }
            set
            {
                _CashBankAccount = value;
                RaisePropertyChanged("CashBankAccount");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public ObservableCollection<CashBankInDetailDTO> CashBankInDetails
        {
            get { return _CashBankInDetails; }
            set
            {
                if (value==null || ReferenceEquals(value,CashBankInDetails))return;
                _CashBankInDetails = value;
                RaisePropertyChanged("CashBankInDetails");
                RefreshNumber();
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        public Decimal TotalDebit
        {
            get { return _TotalDebit; }
            set
            {
                _TotalDebit = value;
                RaisePropertyChanged("TotalDebit");
            }
        }

        public Decimal TotalKredit
        {
            get { return _TotalKredit; }
            set
            {
                _TotalKredit = value;
                RaisePropertyChanged("TotalKredit");
            }
        }

        public Decimal Balance
        {
            get { return _Balance; }
            set
            {
                _Balance = value;
                RaisePropertyChanged("Balance");
            }
        }
        private void RefreshNumber()
        {
            CashBankInDetails.CollectionChanged += CashBankInDetails_CollectionChanged;
            int number = 0;
            foreach (var detailDto in CashBankInDetails)
            {
                number += 1;
                detailDto.No = number;
            }
        }

        private void CashBankInDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (var detailDto in CashBankInDetails)
            {
                number += 1;
                detailDto.No = number;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}